package api

import (
	"gitee.com/cold777/card_house/global"
	"gitee.com/cold777/card_house/pkg/app"
	"gitee.com/cold777/card_house/pkg/errcode"
	"gitee.com/cold777/card_house/third_party/wechat"
	"github.com/gin-gonic/gin"
)

func GetToken(c *gin.Context) {
	response := app.NewResponse(c)
	req := &wechat.AccessTokenRequest{
		GrantType: "client_credential",
		Appid:     global.WechatSetting.Appid,
		Secret:    global.WechatSetting.Secret,
	}
	token, err := wechat.GetAccessToken(c, req)
	if err != nil {
		global.Log.Errorf(c, "GetAccessToken error: %v", err)
		response.ToErrorResponse(errcode.UnauthorizedTokenGenerate.WithDetails(err.Error()))
		return
	}

	response.ToResponse(gin.H{
		"token": token,
	})
}
